
import ca.newcommerce.youtube.data.VideoData;

import mx.collections.ArrayCollection;

public var defaultPlaylistkey:String;

private function getDefaultPlaylistKey(shouldUploadSongs:Boolean):void
{
	var index:uint = 0;
	var didFound:Boolean = false;
	
	while(!didFound && index < this.playlistArr.length)
	{
		if (this.playlistArr[index].isDefault == true)
		{
			this.defaultPlaylistkey = this.playlistArr[index].keyString;
			didFound = true;
		}
		index++
	}
	
	if (shouldUploadSongs)
	{
		// get all the default playlist songs and show them
		this.retrievePlaylist(this.defaultPlaylistkey);
		playlistListComp.setDefaultPlaylistAsChosen();
	}
}

public function isDefaultPlaylist(plKey:String):Boolean{
	return plKey==defaultPlaylistkey;
}

public function showPlaylist(plKey:String, songsArr:ArrayCollection):void
{
	curr_plylist.loadPlaylist(plKey,songsArr);
}

public function addSongToPlaylist(videoToAdd:VideoData, plKey:String):void
{
	var songObj:Object = {title:videoToAdd.title, duration:videoToAdd.duration, id:videoToAdd.actualId,
		name:videoToAdd.title, time:parseTime(videoToAdd.duration),
		artist:"", album:"", genre:"", path:""};
	
	if (plKey == null)
	{
		plKey = this.defaultPlaylistkey;
	}
	
	// calling to the function which will call the server 
	this.addSong(plKey, songObj);
}

private function addSongToCurrPlaylist(plKey:String, newSong:Object, newSongKey:String):Number
{
	// if the added song was added to the current playlist
	if (plKey == curr_plylist.getCurrentPlaylist())
	{
		var songObj:Object = {title:newSong.title, duration:newSong.duration, id:newSong.id,
			name:newSong.title, time:newSong.time,
			artist:newSong.artist, album:newSong.album, genre:newSong.genre,
			keyString:newSongKey, path:newSong.path, mark:1};
		if (newSong.name != "")
		{
			songObj.name = newSong.name;
		}
		
		return this.curr_plylist.addSongToThisPlaylist(songObj);
	}
	return -1;
}
private var deletedSongIndex:Number;
public function deleteSongFromPlaylist(plKey:String, songKey:String, deleteIndex:Number):void
{
	//actionsListComp.debug.text = "deleteSongFromPlaylist " + deleteIndex.toString();
	this.deletedSongIndex = deleteIndex;
	this.deleteSong(plKey, songKey);
}

private function removeSongFromCurrPlaylist():void
{
	//actionsListComp.debug.text = "removeSongFromCurrPlaylist";
	curr_plylist.deleteSongFromThisPlaylist(deletedSongIndex);
}

public function editSongData(songObj:Object, songIndex:Number):void
{
	// save the info on the server
	this.editSongInfo(songObj);
	
	// update the object in the playlist data grid
	curr_plylist.changeSongObj(songIndex, songObj);
}

public function changeVideoId(songObj:Object, songIndex:Number):void
{
	this.updateVideoID(songObj.keyString, songObj.id);
	
	// update the object in the playlist data grid
	curr_plylist.changeSongObj(songIndex, songObj);
}

public function parseTime(seconds:Number):String {
	var secondsStr:String = new String();
	var min:Number = Math.floor(seconds/60)
	var sec:Number = Math.floor(seconds%60);
	secondsStr = min + ":" + ((sec<10) ? ("0" + sec) : sec); 
	return secondsStr;
}